package com.woniuxy.depot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.depot.mapper.DepotInStockMapper;
import com.woniuxy.depot.model.DTO.Condition;
import com.woniuxy.depot.model.DTO.InStockDTO;
import com.woniuxy.depot.model.DTO.Statistics;
import com.woniuxy.depot.model.VO.StockGoodsVO;
import com.woniuxy.depot.model.VO.StockGoodssVO;
import com.woniuxy.depot.model.entity.DepotInStock;
import com.woniuxy.depot.service.DepotInStockItemService;
import com.woniuxy.depot.service.DepotInStockService;
import com.woniuxy.depot.service.DepotStockManageService;
import com.woniuxy.util.SnowflakeIdWorker;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DepotInStockServiceImpl implements DepotInStockService {
    @Resource
    private DepotInStockMapper inStockMapper;
    @Resource
    private DepotInStockItemService itemService;
    @Resource
    private DepotStockManageService stockManageService;
    @Override
    public Page<DepotInStock> all(int current, int size) {
        return inStockMapper.selectPage(new Page<>(current,size),null);
    }

    @Override
    public List<DepotInStock> searchByGoodsCode(String code) {
        List<Integer> inStockIds = itemService.getInStockIdsByCode(code);
        QueryWrapper<DepotInStock> wrapper = new QueryWrapper<>();
        wrapper.in("id",inStockIds);

        return inStockMapper.selectList(wrapper);
    }

    @Override
    public List<DepotInStock> searchByCondition(Condition condition) {
        QueryWrapper<DepotInStock> wrapper = new QueryWrapper<>();
        wrapper.eq(!("".equals(condition.getFormNo())), "form_no",condition.getFormNo()).between(!("".equals(condition.getStartTime())||"".equals(condition.getEndTime())),"create_time",condition.getStartTime(),condition.getEndTime());
        return inStockMapper.selectList(wrapper);
    }

    @Transactional
    @Override
    public Boolean updateState(String formNo, Integer state) {
        DepotInStock inStock = new DepotInStock().setState(state).setAuditor(this.getAccount());
        inStockMapper.update(inStock,new UpdateWrapper<DepotInStock>().eq("form_no",formNo));
        return true;
    }

    @Transactional
    @Override
    public Boolean insert(InStockDTO inStockDTO) {
        //添加入库单
        DepotInStock depotInStock = new DepotInStock().setType(inStockDTO.getType())
                .setCustomer(inStockDTO.getCustomer()).setDepotId(inStockDTO.getDepotId())
                .setProcurementId(inStockDTO.getProcurementId()).setFormMaker(inStockDTO.getFormMaker())
                .setState(inStockDTO.getState()).setAbc(inStockDTO.getAbc()).setCreateTime(new Date())
                .setFormNo(String.valueOf(SnowflakeIdWorker.newId()));
        inStockMapper.insert(depotInStock);
        //添加入库商品表
        for (int i = 0; i < inStockDTO.getItems().size(); i++) {
            inStockDTO.getItems().get(i).setInStockId(depotInStock.getId());

            if(stockManageService.hasGoods(inStockDTO.getDepotId(),inStockDTO.getItems().get(i).getItemCode())){
                //已有商品，增加库存
                stockManageService.updateNum(inStockDTO.getDepotId(),inStockDTO.getItems().get(i));
            }else {
                //新商品，添加商品管理
                stockManageService.addStock(inStockDTO.getDepotId(),inStockDTO.getItems().get(i));
            }

        }
        //批量新增inStockItem
        itemService.insert(inStockDTO.getItems());
        return true;
    }

    @Transactional
    @Override
    public List<StockGoodssVO> detail(Integer id) {
        return inStockMapper.detail(id);
    }

    @Override
    public List<Statistics> statistics() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式

        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);
        String day = df.format(c.getTime());

        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastday =  df.format(ca.getTime());

        return inStockMapper.queryByDate(day,lastday);
    }

    private String getAccount(){
        return  SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
    }
}
